package utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtils {


    private static DataSource ds = null;
    static{
        //1. 加载配置文件
        Properties pt = new Properties();
        InputStream ras = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
        try {
            pt.load(ras);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            ds = DruidDataSourceFactory.createDataSource(pt);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static DataSource getDataSource() {
        return ds;
    }

    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    public static void close(Connection conn, Statement stmt) throws SQLException {
        if(conn != null){
            conn.close();
        }

        if(stmt != null){
            stmt.close();
        }
    }
}
